//
//  ValidationSafetyCodeViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/2.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "ValidationSafetyCodeViewController.h"
#import "SetSafetyCodeViewController.h"
@interface ValidationSafetyCodeViewController ()
@property (nonatomic,assign) BOOL usePwd;
@end

@implementation ValidationSafetyCodeViewController
SINGLETON_FOR_CLASS(ValidationSafetyCodeViewController)
+ (void)showValidation{
    if (![self isShow]) {
        [ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view.frame = kAppWindow.frame;
        [kAppWindow addSubview:[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view];
    }
}
+(BOOL)isShow{
    return [kAppWindow.subviews containsObject:[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view];
}
+ (void)check{
    if (kIsNeedFaceIDOrTouchIDUnLock && ![self sharedValidationSafetyCodeViewController].usePwd) {
        [TDTouchID td_showTouchIDWithDescribe:nil BlockState:^(TDTouchIDState state, NSError *error) {
            if (state == TDTouchIDStateSuccess) {
                [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController] backBtnClicked];
            }else if(state == TDTouchIDStateInputPassword){
                [self sharedValidationSafetyCodeViewController].usePwd = YES;
                [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController] usePassword];
            }else if (state == TDTouchIDStateNotSupport) {    //不支持TouchID
                [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view makeToast: kIsIphoneX?XLocalized(@"当前设备不支持FaceID, 请使用密码"):XLocalized(@"当前设备不支持TouchID, 请使用密码")];
            }else if(state == TDTouchIDStateUserCancel){
                [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view makeToast: XLocalized(@"您已取消验证") ];
                [self sharedValidationSafetyCodeViewController].usePwd = YES;
            }else{
                [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view makeToast: error.localizedDescription];
            }
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_interactivePopDisabled=YES;
    self.navigationBarHidden = YES;
    self.customNavigationBar.height = kStatusBarHeight;
    UILabel *tipLab = [UILabel new];
    tipLab.font = kBoldFont(23);
    tipLab.textColor = UIColorHex(333333);
    tipLab.text = XLocalized(@"欢迎回来");
    [self.contentView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedHeight(30));
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(AdaptedWidth(15));
    }];
    
    UIButton *btn = [UIButton new];
    if (kIsNeedFaceIDOrTouchIDUnLock) {
        [btn setImage:kIsIphoneX?IMAGE_NAMED(@"face_id"):IMAGE_NAMED(@"touch_id") forState:UIControlStateNormal];
    }else{
        [btn setImage:IMAGE_NAMED(@"code_id") forState:UIControlStateNormal];
    }
    [btn setTitle:XLocalized(@"点击开始解锁") forState:UIControlStateNormal];
    [btn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
    btn.titleLabel.font = kDefaultFont(15);
    kWeakSelf(self);

    [btn addTapBlock:^(UIButton * _Nonnull btn) {
        if (kIsNeedFaceIDOrTouchIDUnLock) {
            [TDTouchID td_showTouchIDWithDescribe:nil BlockState:^(TDTouchIDState state, NSError *error) {
                if (state == TDTouchIDStateSuccess) {
                    [weakself backBtnClicked];
                }else if(state == TDTouchIDStateInputPassword){
                    [weakself usePassword];
                }else if (state == TDTouchIDStateNotSupport) {    //不支持TouchID
                    [weakself.view makeToast: kIsIphoneX?XLocalized(@"当前设备不支持FaceID, 请使用密码"):XLocalized(@"当前设备不支持TouchID, 请使用密码")];
                }else if(state == TDTouchIDStateUserCancel){
                    [weakself.view makeToast: XLocalized(@"您已取消验证") ];
                }else{
                    [weakself.view makeToast: error.localizedDescription];
                }
            }];
        }else{
            [weakself usePassword];
        }
    }];
    
    [self.contentView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab.mas_bottom).offset(AdaptedWidth(90));
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(AdaptedHeight(160));
        make.width.mas_equalTo(AdaptedHeight(100));
    }];
    [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:AdaptedHeight(40)];
    
    XThemeFlatButton *bottomBtn=[XThemeFlatButton button];
    [bottomBtn setTitle:XLocalized(@"使用密码验证") forState:UIControlStateNormal];
    [self.contentView addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn.mas_bottom).offset(AdaptedHeight(200));
        make.height.mas_equalTo(AdaptedWidth(40));
        make.width.mas_equalTo(AdaptedWidth(160));
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-AdaptedHeight(60));
    }];
    
    [bottomBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself usePassword];
    }];
}
- (void)backBtnClicked{
    [ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].usePwd = NO;
    [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view removeFromSuperview];
}
- (void)usePassword{
//    return;
    SetSafetyCodeViewController *vc = [SetSafetyCodeViewController validationSafetyType:SafetyCodeTypeValidation codeCallback:^(SetSafetyCodeViewController *codeVc,BOOL success) {
        if (success) {
            [codeVc.navigationController.view removeFromSuperview];
            [codeVc.navigationController removeFromParentViewController];
            [ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].usePwd = NO;
            [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view removeFromSuperview];
        }
    } noDismiss:NO];
    RootNavigationController *nav = [[RootNavigationController alloc]initWithRootViewController:vc];
//    [kAppWindow.rootViewController presentViewController:nav animated:YES completion:nil];
//    RootNavigationController *nav = [[RootNavigationController alloc]initWithRootViewController:vc];
    nav.view.frame = [ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view.frame;
    [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController] addChildViewController:nav];
    [[ValidationSafetyCodeViewController sharedValidationSafetyCodeViewController].view addSubview:nav.view];
    kWeakSelf(vc);
    vc.DismissBlock = ^{
        [weakvc.navigationController.view removeFromSuperview];
        [weakvc.navigationController removeFromParentViewController];
    };
}

@end
